package com.laikan.reader.bookfriend.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.*;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.sharesdk.framework.ShareSDK;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.PublicUtil;
import thirdparty.cn.sharesdk.onekeyshare.OnekeyShare;

public class WebViewActivity extends BasicActivity implements OnClickListener {

    private String url;
    /**
     * 标题栏
     */
//    private CommonTitleLayout ctl_title;
    private WebView webview;
    private LoadingDialog loadingDialog;
    private String title;
    private TextView title_tv;
    private ImageView back_tv;
    Drawable drawable_back_tv;
    private RelativeLayout back_layout;

    /*android2.2.3的bug在js中调这个方法时出现:
     * JNI WARNING: jarray 0x4054d218 points to non-array object (Lja*/

    public void showShare(String title, String desc, String img) {
        String urlmobile = "http://m.laikan.com/huodong/2015Dec";
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        oks.setTitle(title);
        oks.setTitleUrl(urlmobile);
        String str = "";
        if (desc.length() > 119) {
            str = desc.substring(0, 118) + "...";
        }else {
            str = desc;
        }
        oks.setText(str);
        oks.setImageUrl(img);//
        oks.setUrl(urlmobile);
        oks.setComment(img);
        oks.setSite(getString(R.string.app_name));
        oks.setSiteUrl(urlmobile);
        oks.show(this);
    }

    @Override
    public void onClick(View v) {
    }

    @SuppressLint ("JavascriptInterface")
    @Override
    protected void assignViews() {
        setContentView(R.layout.lk_webview);
        loadingDialog = new LoadingDialog(this);
        if (null != loadingDialog && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
        url = getIntent().getStringExtra("mHtmlUrl");
        title = getIntent().getStringExtra("title");
//        ctl_title.setTitle(title);

        webview = (WebView)findViewById(R.id.webview);
        String weburl = "http://www.laikan.com/android/2/festival/egg_frenzy2015_08";
        webview.setWebViewClient(new MyWebViewClient());
        webview.setWebChromeClient(new MyWebChromeClient());

        WebSettings websettings = webview.getSettings();
        websettings.setJavaScriptEnabled(true);//可以使用javascript------!
        webview.addJavascriptInterface(new CallJavaInterface(), "demo");//通过调这个注册
        webview.postUrl(url, ("sd=" + SPUtil.getString("token", "")).getBytes());

//        ctl_title = (CommonTitleLayout)findViewById(ctl_title);
        back_tv = (ImageView) findViewById(R.id.iv_back);
        title_tv = (TextView) findViewById(R.id.tv_title);
        title_tv.setText(title);
        back_layout = (RelativeLayout)findViewById(R.id.back_layout);
    }

    @Override
    protected void initData() {
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext,R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        back_tv.setImageDrawable(drawable_back_tv);
    }

    @Override
    protected void setListener() {
        back_layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class CallJavaInterface {

        public String calljava(String name) {
            return name;
        }
    }

    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            AlertDialog ad = new AlertDialog.Builder(WebViewActivity.this).setTitle("警告对话框").setMessage(message).setPositiveButton("确定", null).create();
            //			ad.show();
            result.confirm();
            return true;
            //return super.onJsAlert(view, mHtmlUrl, message, result);
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            AlertDialog ad = new AlertDialog.Builder(WebViewActivity.this).setTitle("确认对话框").setMessage(message).setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.confirm();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.cancel();
                }
            }).create();
            ad.show();
            return true;
            //return super.onJsConfirm(view, mHtmlUrl, message, result);
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
            EditText et = new EditText(WebViewActivity.this);
            et.setText(defaultValue);

            AlertDialog ad = new AlertDialog.Builder(WebViewActivity.this).setTitle("输入对话框").setMessage(message).setView(et).setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.confirm();//返结果用带参的
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.cancel();
                }
            }).create();
            ad.show();
            return true;
            //return super.onJsPrompt(view, mHtmlUrl, message, defaultValue, result);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                if (null != loadingDialog && loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            }
            super.onProgressChanged(view, newProgress);
        }

    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onLoadResource(WebView view, String url) {

            super.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("share")) {

                Uri uri = Uri.parse(url);
                String title = uri.getQueryParameter("title");
                String desc = uri.getQueryParameter("desc");
                String img = uri.getQueryParameter("img");
                showShare(title, desc, img);
                return true;
            }else if (url.contains("pay")) {

                startActivity(new Intent().setClass(mContext, RechargePage.class));
                return true;
            }
            view.loadUrl(url);
            return true;
        }

    }
}